package maggames.core.loader;

import javax.swing.JFrame;

import maggames.core.interfaces.GameFactory;
import maggames.games.seega.SeegaGameFactory;

import org.apache.log4j.Logger;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.util.Properties;

import javax.swing.JComboBox;
import javax.swing.JButton;
import maggames.core.base.BaseSwingInterface;
import java.awt.FlowLayout;
import java.awt.GridLayout;

/**
 * Main Swing interface for loading and choosing games and their players.
 * 
 * @author BenjaminPLee
 * @version 1.0
 */
public class VisualGameLoader extends JFrame {
	private static final long serialVersionUID = 8065605698479691698L;
	private static final Logger log = Logger.getLogger(VisualGameLoader.class);  //  @jve:decl-index=0:
	private JPanel loaderPanel = null;
	private JLabel lblPlayer1 = null;
	private JComboBox comboPlayer1 = null;
	private JLabel lblPlayer2 = null;
	private JComboBox comboPlayer2 = null;
	private JButton btnPlayGame = null;
	private JComboBox comboGame = null;
	private JLabel lblGame = null;
	private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="482,76"
	private GameFactory gameFactory;
	private JPanel container = null;
	/**
	 * This method initializes 
	 * 
	 */
	public VisualGameLoader() {
		super();
		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(385,150));
        this.setContentPane(getContainer());
        this.setTitle("Mag-Games");
			
	}
	/**
	 * This method initializes loaderPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getLoaderPanel() {
		if (loaderPanel == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 3;
			gridBagConstraints21.gridy = 0;
			lblGame = new JLabel();
			lblGame.setText("Game");
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints12.gridy = 1;
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.gridx = 3;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 3;
			gridBagConstraints11.gridy = 3;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 7;
			gridBagConstraints7.gridy = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridheight = 1;
			gridBagConstraints3.gridwidth = 3;
			gridBagConstraints3.gridx = 5;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 4;
			gridBagConstraints2.gridy = 1;
			lblPlayer2 = new JLabel();
			lblPlayer2.setText("Player 2");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			lblPlayer1 = new JLabel();
			lblPlayer1.setText("Player 1");
			loaderPanel = new JPanel();
			loaderPanel.setLayout(new GridBagLayout());
			loaderPanel.add(lblPlayer1, gridBagConstraints);
			loaderPanel.add(getComboPlayer1(), gridBagConstraints1);
			loaderPanel.add(lblPlayer2, gridBagConstraints2);
			loaderPanel.add(getComboPlayer2(), gridBagConstraints3);
			loaderPanel.add(getBtnPlayGame(), gridBagConstraints11);
			loaderPanel.add(getComboGame(), gridBagConstraints12);
			loaderPanel.add(lblGame, gridBagConstraints21);
		}
		return loaderPanel;
	}
	/**
	 * This method initializes comboPlayer1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getComboPlayer1() {
		if (comboPlayer1 == null) {
			comboPlayer1 = new JComboBox();
			comboPlayer1.addItem("Human");
		}
		return comboPlayer1;
	}
	/**
	 * This method initializes comboPlayer2	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getComboPlayer2() {
		if (comboPlayer2 == null) {
			comboPlayer2 = new JComboBox();
			comboPlayer2.addItem("Human");
		}
		return comboPlayer2;
	}
	/**
	 * This method initializes btnPlayGame	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnPlayGame() {
		if (btnPlayGame == null) {
			btnPlayGame = new JButton();
			btnPlayGame.setText("Play");
			btnPlayGame.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					gameFactory = new SeegaGameFactory();
					gameFactory.getEngine().initialize(new Properties());
					startGame();
				}
			});
		}
		return btnPlayGame;
	}
	/**
	 * This method initializes comboGame	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getComboGame() {
		if (comboGame == null) {
			comboGame = new JComboBox();
			comboGame.addItem("Seega");
		}
		return comboGame;
	}
	
	public void startGame() {
		this.setContentPane(gameFactory.getSwingInterface());
		this.setSize(getContentPane().getSize());
		this.setBounds(0,0,getContentPane().getWidth(), getContentPane().getHeight());
		this.repaint();
	}
	/**
	 * This method initializes container	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getContainer() {
		if (container == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			container = new JPanel();
			container.setLayout(gridLayout);
			container.add(getLoaderPanel(), null);
			container.addComponentListener(new java.awt.event.ComponentAdapter() {
				public void componentResized(java.awt.event.ComponentEvent e) {
					System.out.println("componentResized()"); // TODO Auto-generated Event stub componentResized()
				}
			});
		}
		return container;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
